package edu.pku.sei.transformation.editor.actions;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchWindow;

import edu.pku.sei.transformation.editor.model.execution.TransformationExecution;
import edu.pku.sei.transformation.editor.views.console.ConsoleViewManager;
import edu.pku.sei.transformation.editor.views.problem.ProblemView;
import edu.pku.sei.transformation.editor.views.progreassmonitor.ThreadSafeProgressMonitor;
import edu.pku.sei.transformation.execution.ExecutionController;
import edu.pku.sei.transformation.execution.ExecutionEnviroment;

public class ExecuteTransformationAction extends Action {
	private TransformationExecution execution;

	public ExecuteTransformationAction(TransformationExecution execution) {
		this.execution = execution;
		this.setText("execute transformation");
	}

	@Override
	public void run() {
		ProblemView.clearMarks(execution.getModel());
		ProblemView.showView();
		ConsoleViewManager.showView();
		WorkbenchWindow workbenchWindow = (WorkbenchWindow) PlatformUI
				.getWorkbench().getActiveWorkbenchWindow();
		IActionBars bars = workbenchWindow.getActionBars();
		IStatusLineManager mgr = bars.getStatusLineManager();
		IProgressMonitor monitor = mgr.getProgressMonitor();
		//ThreadSafeProgressMonitor smonitor = new ThreadSafeProgressMonitor(monitor);

		try {
			//ExecutionEnviroment env = new ExecutionEnviroment(execution
			//		.getModel());
			ExecutionController controller = new ExecutionController(execution);
			controller.run(monitor);
		} catch (Exception e) {
			monitor.setCanceled(true);
			monitor.done();
			e.printStackTrace();
		}
	}
}
